<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容 头部 -->
      <el-card class="tree-card">
        <TreeTools
          :tree-node="company"
          :is-root="true"
          @addDepts="addDepts"
        />
        <!-- ————————————————————————————————————华丽的分割线—————————————————————————————————— -->
        <!-- 放置一个el-tree -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <TreeTools
            slot-scope="{data}"
            :tree-node="data"
            @addDepts="addDepts"
            @delDepts="getDepartments"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 新增弹层 -->
    <AddDept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
      @changeDialog="showDialog"
    />
    <!-- 原始写法 -->
    <!-- <AddDept
      :tree-node="node"
      @update:showDialog="showDialog = $event"
      @addDepts="getDepartments"
      @changeDialog="showDialog"
    /> -->
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tarnListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { }, // 头部数据
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false, // 默认不显示
      node: null,
      loading: false // 用来控制进度弹层的显示和隐藏
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const data = await getDepartments()
      this.company = { name: data.companyName, manager: '负责人', id: '' }
      this.departs = tarnListToTreeData(data.depts, '')
      this.loading = false
      console.log(data)
    },
    // 监听 tree-tools 中触发的点击添加子部门的事件
    addDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node
    },
    test(value) {
      this.showDialog = value
    },
    editDepts(node) {
      this.showDialog = true // 弹出层
      this.node = node
      // 应该在这里调用获取部门详情的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
